<?php
namespace jy_weishop\activity;
defined('BY_JYA') or exit('error');
/*
	整点预售
*/
class presell extends \plugin{
	public function get_active($goods_id,$active_id){
		$active=$this->get('activity_yushou',array('id'=>$active_id));
		if(!$active){
			return false;
		}
		if($active['stop'] || $active['start_time']>TIMESTAMP || $active['end_time']<TIMESTAMP){
			return false;
		}
		$active_price=round($active['price'],2);	
		return array(
			'title'=>'预售',
			'price'=>$active_price,
			'rest_time'=>$this->model()->date($active['end_time']-TIMESTAMP),
			'sendtime'=>$active['sendtime'],
		);
	}
	public function check_stock($goods_id,$active_id,$num){
		return true;
	}
	public function buy($goods_id,$active_id,$num){
		$active=$this->get('activity_yushou',array('id'=>$active_id));
		if(!$active){
			return false;
		}
		if($active['start_time']>TIMESTAMP || $active['end_time']<TIMESTAMP){
			return false;
		}
		if($active['num']<$num){
			exi('该活动每人只能购买'.$active['num'].'件商品！','error');
		}
		return true;
	}
	//$goods_id,$active_id,$num,$ordersn
	public function order_create($params){
		$log=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'goods_id'=>$params['goods_id'],
			'active_id'=>$params['active_id'],
			'ordersn'=>$params['ordersn'],
			'num'=>$params['num'],
			'price'=>$params['goods_price'],
			'createtime'=>TIMESTAMP,
		);
		$this->insert('activity_yushou_log',$log);
	}
}